﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Jhong.FunMVC.Razor
{
    using Westwind.RazorHosting;

    public static class SelectExtensions
    {
        public static RawString DropDownList(this RazorHTMLHelper helper, string name)
        {
            return helper.DropDownList(name, null);
        }

        public static RawString DropDownList(this RazorHTMLHelper helper, string name, IEnumerable<SelectListItem> selectList)
        {
            return helper.DropDownList(name, selectList, null);
        }

        public static RawString DropDownList(this RazorHTMLHelper helper, string name, IEnumerable<SelectListItem> selectList, object htmlAttributes)
        {
            var htmlsb = new StringBuilder();
            htmlsb.Append(string.Format(@"<select name='{0}' id='{0}' {1} >", name, helper.AnonymousObjectToHtmlString(htmlAttributes)));
            if (null != selectList && selectList.Count() > 0)
                foreach (var s in selectList)
                    htmlsb.Append(string.Format(@"<option value='{0}' {2} >{1}</option>", s.Value, s.Text, s.Selected ? "selected='selected'" : string.Empty));
            htmlsb.Append("</select>");
            return new RawString(htmlsb.ToString());
        }
    }


    public class SelectListItem
    {
        public string Text { get; set; }

        public string Value { get; set; }

        public bool Selected { get; set; }
    }
}
